#include "scrambled_symbols.h"
#include "priv/gisusswfileimpl.hxx"
#include "gisusswfile.hxx"

#include <stdio.h>  // For sscanf
#include <string.h>  // For memcpy
#include <iostream>

using namespace std;

GisusSWFileImpl::GisusSWFileImpl(const char **swdata)
  : _id(0),_filename(),_size(0),
    _nbofchunks(0),_signature(0),_sig_size(0),_version(0),
    _globalversion(0),_rdproducttype(),_comproducttype(),
    _productfamily(),_productcomref()
{
  ExtractData(swdata);
}

GisusSWFileImpl::GisusSWFileImpl(const GisusSWFileImpl &model)
  : _id(model._id), _filename(model._filename),
    _size(model._size), _nbofchunks(model._nbofchunks),
    _signature(0), _sig_size(model._sig_size), 
    _version(model._version), _globalversion(model._globalversion),
    _rdproducttype(model._rdproducttype),_comproducttype(model._comproducttype),
    _productfamily(model._productfamily),_productcomref(model._productcomref)
{
  _signature=new tByte[_sig_size];
  memcpy(_signature,model._signature,_sig_size);
}

GisusSWFileImpl::~GisusSWFileImpl()
{
  if (_signature)
    {
      delete [] _signature;
    }
  _signature=0;
}

bool GisusSWFileImpl::ExtractData(const char **swdata)
{
  sscanf(swdata[0],"%u",&_id);            // software.id
  _filename=string(swdata[1]);            // filename
  sscanf(swdata[2],"%u",&_size);          // size
  sscanf(swdata[3],"%u",&_nbofchunks);    // chunknb
  sscanf(swdata[4],"%u",&_sig_size);      // sigsize

  _signature=new tByte[_sig_size];          
  memcpy(_signature,swdata[5],_sig_size); // signature

  _version=0;  // Not used now.

  sscanf(swdata[6],"%u",&_globalversion); // global version
  _rdproducttype=string(swdata[7]);       // producttype.rdname
  _comproducttype=string(swdata[8]);      // producttype.comname
  _productfamily=string(swdata[9]);      // productfamily.name
  _productcomref=string(swdata[10]);      // product.comref

  return true;
}

///////////////////////////////////////////////////////////////////////////////
//
///////////////////

GisusSWFile::GisusSWFile(const char **swdata)
  : _impl(0)
{
  _impl=new GisusSWFileImpl(swdata);
}

GisusSWFile::GisusSWFile(const GisusSWFile &model)
  : _impl(0)
{
  _impl=new GisusSWFileImpl(*model._impl);
}

GisusSWFile::~GisusSWFile()
{
  if (_impl)
    {
      delete _impl;
    }
  _impl=0;
}

tUInt32 GisusSWFile::Id() const
{
    return _impl->Id();
}

const string &GisusSWFile::Filename() const
{
    return _impl->Filename();
}

tUInt32 GisusSWFile::Size() const
{
    return _impl->Size();
}

tUInt32 GisusSWFile::NbOfChunks() const
{
    return _impl->NbOfChunks();
}

tUInt32 GisusSWFile::SigSize() const
{
  return _impl->SigSize();
}

tByte *GisusSWFile::Signature() const
{
  return _impl->Signature();
}

tUInt32 GisusSWFile::Version() const
{
  return _impl->Version();
}

tUInt32 GisusSWFile::GlobalVersion() const
{
  return _impl->GlobalVersion();
}

const string &GisusSWFile::RDProductType() const
{
  return _impl->RDProductType();
}

const string &GisusSWFile::ComProductType() const
{
  return _impl->ComProductType();
}

const string &GisusSWFile::ProductFamily() const
{
  return _impl->ProductFamily();
}

const string &GisusSWFile::ComRef() const
{
  return _impl->ComRef();
}
